package org.eclipse.epsilon.emc.spreadsheets.csv.delete;

import static org.junit.Assert.assertTrue;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.eclipse.epsilon.common.util.FileUtil;
import org.eclipse.epsilon.emc.spreadsheets.SpreadsheetRow;
import org.eclipse.epsilon.emc.spreadsheets.csv.CsvModel;
import org.eclipse.epsilon.emc.spreadsheets.csv.CsvRow;
import org.eclipse.epsilon.emc.spreadsheets.csv.CsvWorksheet;
import org.eclipse.epsilon.eol.EolModule;
import org.eclipse.epsilon.eol.exceptions.EolRuntimeException;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class NoHeaderDeleteRowTest
{
	private static CsvModel model = null;
	private static String filePath = "resources/CsvFile.txt";

	@BeforeClass
	public static void setUpFileAndLoad() throws Exception
	{
		FileUtil.setFileContents("", new File(filePath));
		setUp();
	}

	private static void setUp() throws Exception
	{
		model = new CsvModel();
		model.setFile(filePath);
		model.setConfigurationFile("resources/CsvFileTestConfig.xml");
		model.setHasHeader(false);
		model.setStoredOnDisposal(false);
		model.setName("MODEL");
		model.load();
	}

	@Before
	public void setUpRows() throws Exception
	{
		List<SpreadsheetRow> rows = (List<SpreadsheetRow>) model.getAllOfKind(CsvWorksheet.NAME);
		for (SpreadsheetRow row : rows)
		{
			row.getWorksheet().deleteRow(row);
		}

		for (int i = 0; i < 100; i++)
		{
			Map<String, Object> values = new HashMap<String, Object>();
			values.put("c_0", i);
			values.put("c_1", "value1");
			values.put("c_2", "value2");
			values.put("c_3", "value3");
			values.put("c_4", "value4");
			values.put("c_5", "value5");
			model.createInstance(CsvWorksheet.NAME, values);
		}
	}

	@Test
	public void deleteAllRows() throws EolRuntimeException
	{
		List<SpreadsheetRow> rows = (List<SpreadsheetRow>) model.getAllOfKind(CsvWorksheet.NAME);
		assertTrue(CollectionUtils.isNotEmpty(rows));
		assertTrue(rows.size() == 100);
		for (SpreadsheetRow row : rows)
		{
			row.getWorksheet().deleteRow(row);
		}
		rows = (List<SpreadsheetRow>) model.getAllOfKind(CsvWorksheet.NAME);
		assertTrue(rows.size() == 0);
	}

	@Test
	public void deleteFirstRow() throws EolRuntimeException
	{
		List<SpreadsheetRow> rows = (List<SpreadsheetRow>) model.getAllOfKind(CsvWorksheet.NAME);
		assertTrue(CollectionUtils.isNotEmpty(rows));
		assertTrue(rows.size() == 100);
		SpreadsheetRow rowToDelete = rows.get(0);
		rowToDelete.getWorksheet().deleteRow(rowToDelete);
		rows = (List<SpreadsheetRow>) model.getAllOfKind(CsvWorksheet.NAME);
		assertTrue(rows.size() == 99);
		for (int i = 1; i < rows.size(); i++)
		{
			CsvRow row = (CsvRow) rows.get(i - 1);
			assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("col0")).equals(String.valueOf(i)));
			assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("col1")).equals("value1"));
			assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("col2")).equals("value2"));
			assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("col3")).equals("value3"));
			assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("col4")).equals("value4"));
			assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("col5")).equals("value5"));
		}
	}

	@Test
	public void deleteLastRow() throws EolRuntimeException
	{
		List<SpreadsheetRow> rows = (List<SpreadsheetRow>) model.getAllOfKind(CsvWorksheet.NAME);
		assertTrue(CollectionUtils.isNotEmpty(rows));
		assertTrue(rows.size() == 100);
		SpreadsheetRow rowToDelete = rows.get(99);
		rowToDelete.getWorksheet().deleteRow(rowToDelete);
		rows = (List<SpreadsheetRow>) model.getAllOfKind(CsvWorksheet.NAME);
		assertTrue(rows.size() == 99);
		for (int i = 0; i < rows.size(); i++)
		{
			CsvRow row = (CsvRow) rows.get(i);
			assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("col0")).equals(String.valueOf(i)));
			assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("col1")).equals("value1"));
			assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("col2")).equals("value2"));
			assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("col3")).equals("value3"));
			assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("col4")).equals("value4"));
			assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("col5")).equals("value5"));
		}
	}

	@Test
	public void deleteAllRowsWithEOL() throws Exception
	{
		final EolModule module = new EolModule();
		module.parse("delete MODEL!CSV.all;");
		module.getContext().getModelRepository().addModel(model);
		module.execute();

		List<SpreadsheetRow> rows = (List<SpreadsheetRow>) model.getAllOfKind(CsvWorksheet.NAME);
		assertTrue(rows.size() == 0);
	}

	@Test
	public void deleteFirstRowWithEOL() throws Exception
	{
		final EolModule module = new EolModule();
		module.parse("delete MODEL!CSV.all.first();");
		module.getContext().getModelRepository().addModel(model);
		module.execute();

		List<SpreadsheetRow> rows = (List<SpreadsheetRow>) model.getAllOfKind(CsvWorksheet.NAME);
		assertTrue(rows.size() == 99);
		for (int i = 1; i < rows.size(); i++)
		{
			CsvRow row = (CsvRow) rows.get(i - 1);
			assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("col0")).equals(String.valueOf(i)));
			assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("col1")).equals("value1"));
			assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("col2")).equals("value2"));
			assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("col3")).equals("value3"));
			assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("col4")).equals("value4"));
			assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("col5")).equals("value5"));
		}
	}

	@Test
	public void deleteLastRowWithEOL() throws Exception
	{
		final EolModule module = new EolModule();
		module.parse("delete MODEL!CSV.all.last();");
		module.getContext().getModelRepository().addModel(model);
		module.execute();

		List<SpreadsheetRow> rows = (List<SpreadsheetRow>) model.getAllOfKind(CsvWorksheet.NAME);
		assertTrue(rows.size() == 99);
		for (int i = 0; i < rows.size(); i++)
		{
			CsvRow row = (CsvRow) rows.get(i);
			assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("col0")).equals(String.valueOf(i)));
			assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("col1")).equals("value1"));
			assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("col2")).equals("value2"));
			assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("col3")).equals("value3"));
			assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("col4")).equals("value4"));
			assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("col5")).equals("value5"));
		}
	}

}
